﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-15 14:51:18
 * 功能：单个画册对象
**************************************************************************************/

using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.HallPanel
{
    public class GalleryItem : MonoBehaviour
    {
        private Button m_Btn;
        private Toggle m_CollectBtn;
        private Image m_Icon;
        private GameDataModel.Level m_Data;
        private GalleryView m_RootView;
        private EGalleryType m_Type;
        public GalleryItem Previous;
        public GalleryItem Next;

        private void Find()
        {
            m_Btn = GetComponent<Button>();
            m_CollectBtn = transform.Find("CollectBtn").GetComponent<Toggle>();
            m_Icon = transform.Find("Mask/Icon").GetComponent<Image>();
            m_Btn.onClick.AddListener(OnClickBtn);
            m_CollectBtn.onValueChanged.AddListener(OnCollectBtnChanged);
        }

        private void OnClickBtn()
        {
            if (m_Data == null) return;
            // 打开查看图片界面，TODO：
            UIUtility.DisplayPanel(PreviewPanel.PreviewPanelView.PANEL_KEY, "UI/PreviewPanel/Prefabs/PreviewPanel", data: this);
        }

        private void OnCollectBtnChanged(bool isOn)
        {
            if (m_Data == null) return;
            m_Data.collectStatus = isOn ? ECollectStatus.Collected : ECollectStatus.Uncollected;
            if (m_Type == EGalleryType.Liked)
            {
                if (Previous != null)
                    Previous.Next = Next;
                if (Next != null)
                    Next.Previous = Previous;

                Previous = null;
                Next = null;
                m_RootView.DestroyItem(this);
            }
        }

        public void Init(GameDataModel.Level data, GalleryView rootView, EGalleryType type)
        {
            if (m_Btn == null) Find();
            m_Type = type;
            m_RootView = rootView;
            m_Data = data;
            //m_Btn.interactable = m_Data.type == ELevelType.Special;
            m_CollectBtn.SetIsOnWithoutNotify(data.collectStatus == ECollectStatus.Collected);
            if (data.level == 1 || data.finalSelectIdx == -1)
                m_Icon.sprite = AssetCacheServer.Instance.GetDefaultBg();
            else
            {
                var info = m_Data.assetsInfo[m_Data.finalSelectIdx];
                m_Icon.sprite = AssetCacheServer.Instance.GetSprite(info.type, info.id);
            }
            var size = UIUtility.CalcSizeToFill(m_Icon.transform.parent as RectTransform, m_Icon.sprite.texture);
            m_Icon.rectTransform.sizeDelta = new Vector2(size.width, size.height);
        }

        public void Clear()
        {
            m_Icon.sprite = null;
            m_Data = null;
            Previous = null;
            Next = null;
        }

        public void UpdateCollectBtnStatus(bool isOn)
        {
            m_CollectBtn.isOn = isOn;
        }

        public Sprite Icon => m_Icon.sprite;

        public GameDataModel.Level Data => m_Data;

        public EGalleryType Type => m_Type;
    }
}